/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.algorithm.repeatnum;

import com.hyts.algorithm.Solution;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.algorithm.repeatnum
 * @author:LiBo/Alex
 * @create-date:2019-10-30 20:14
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class TestRepeatNum implements Solution {


@Test
    @Override
    public void solution1() {

        String A = "abcd",B = "cdcabcd";

        //计算外层循环和内层循环
        char[] outterCycle = B.toCharArray();
        char[] innerCycyle = A.toCharArray();
        boolean finded = false;
        int repeatNum = 0;
        int lastIndex = 0;
        for(int i = 0 ; i<outterCycle.length && repeatNum!=-1; i++){
            finded = false;
            Character element = outterCycle[i];
            for(int j = 0 ; j < innerCycyle.length ; j++){
                if(element.equals(innerCycyle[j])){
                    System.out.println(i+","+j+","+lastIndex);
                    if ( i == 0 || j == lastIndex+1 || (j == 0 && lastIndex == innerCycyle.length-1)){
                        finded = true;
                        if( i!=0 && ((i+1)!= innerCycyle.length) && (i+1)%innerCycyle.length == 0){
                            repeatNum++;
                        }else if(i == outterCycle.length-1 && j<innerCycyle.length-1){
                            repeatNum++;
                        }else if(i == 0 && j!=i){
                            repeatNum++;
                        }
                    }
                    lastIndex = j;
                }
            }
            if(!finded){
                repeatNum = -1;
            }
        }
        System.out.println(repeatNum);
    }

    @Test
    @Override
    public void solution2() {
        int[] a = {1,2,3,4,5,6,7,1,2,3,4,5,6,7,9,10,31,20,20,31,0,9,7,10};
        boolean flag = true;
        int count = 0;
            for(int element:a){
                int i;
                flag = true;
                count++;
                for(i=0;i<a.length && flag;i++){
                        if(element == a[i] && count-1 != i){
                            flag = false;
                        }
                }
                if(i == a.length && flag){
                    System.out.println( element);
                }
            }
    }

    @Test
    @Override
    public void solution3() {
        String str = "abcd";
        String [] sources = {"cd","a"}, targets = {"eee","ffff"};
        int [] index = {0,2};
        StringBuilder stringBuilder = new StringBuilder();
        for(int i = 0 ; i< sources.length; i++){
            String element = sources[i];
            int length = element.length();
            if(length == 1) {
                for (int k = 0; k < str.length(); k = k + 1) {
                    if (index[i]>=k && element.equals(String.valueOf(str.charAt(k)))) {
                        stringBuilder.append(targets[i]);
                    }else {
                        stringBuilder.append(str.charAt(k));
                    }
                }
            }else if(length > 1 && index[i]>=i){

                int pointer = index[i];

                int nextPointer = -2;

                boolean flag = true;

                while((pointer = str.indexOf(element,pointer)) > -1 && flag){
                    if(nextPointer == -2 || nextPointer > pointer){
                        nextPointer = pointer;
                        pointer +=length;
                        stringBuilder.append(targets[i]);
                    }else{
                        flag = false;
                    }
                }

            }
        }
        System.out.println();
    }


    public static void main(String[] args){
            String str = "abcd";
            String [] sources = {"a","cd"}, targets = {"eee","ffff"};
            int [] index = {0,2};
            StringBuilder stringBuilder = new StringBuilder(str);
            for(int i = 0 ; i< sources.length; i++){
                String element = sources[i];
                if(element.length() == 1){
                    for(int j = 0 ; j< str.toCharArray().length ; j++){
                        if(String.valueOf(str.charAt(j)).equals(element)){
                            stringBuilder.replace(j,j+1,"").insert(j,targets[i]);
                        }
                    }
                }else{
                    for(int pointer=0;(pointer = stringBuilder.indexOf(element,index[i]))>-1;){
                        stringBuilder.replace(pointer,pointer+element.length(),targets[i]);
                    }
                }
            }
        System.out.println(stringBuilder.toString());
    }
}
